package com.java.laixiangdong.express.controller;

import com.java.laixiangdong.express.bean.Express;
import com.java.laixiangdong.express.bean.ExpressPlace;
import com.java.laixiangdong.express.dao.Dao;
import com.java.laixiangdong.express.view.View;

import java.io.*;

/**
 * @author laixiangdong
 */
public class Main {
    public static void main(String[] args) throws IOException {
        View v = View.getInstance();
        Dao dao = Dao.getInstance();
        dao.readFile();
        v.welcome();
        while (true) {
            int s = v.start();
            if (s == 1) {
                switch (v.choose()) {
                    case 1:
                        int odd = v.inputNewExpressOdd();
                        String company = v.inputExpressCompany();
                        Express express = dao.newExpress(odd, company);
                        System.out.println("取件码为：" + express.getCode());
                        break;
                    case 2:
                        int odd2 = v.inputDelExpressOdd();
                        if (dao.remove(odd2)) {
                            System.out.println("删除成功");
                        } else {
                            System.out.println("没有这个快递，删除失败");
                        }
                        break;
                    case 3:
                        int odd3 = v.inputModifyExpressOdd();
                        // 不能直接修改快递属性这样操作了，hashmap会有问题
                        /*
                        Express express3 = dao.searchExpress(odd3);
                        if (express3 != null) {
                            System.out.println("请输入新的快递单号：");
                            express3.setOdd(v.inputExpressOdd());
                            express3.setCompany(v.inputExpressCompany());
                        } else {
                            System.out.println("没有这个快递单号");
                        }
                        */
                        Express express3 = dao.searchExpress(odd3);
                        if (express3 != null) {
                            int oldCode = express3.getCode();
                            ExpressPlace ep = Dao.getExpresses().get(express3);
                            Dao.getExpresses().remove(express3);
                            System.out.println("请输入新的快递单号：");
                            int newOdd = v.inputExpressOdd();
                            String newCompany = v.inputExpressCompany();
                            Express newExpress = new Express(newOdd, oldCode, newCompany);
                            newExpress.setCode(oldCode);
                            Dao.getExpresses().put(newExpress, ep);
                        } else {
                            System.out.println("没有这个快递单号");
                        }
                        break;
                    case 4:
                        dao.showAllExpress();
                }
            } else if (s == 2) {
                int code = v.inputExpressCode();
                if (dao.getExpressOddByCode(code) == -1) {
                    System.out.println("没有这个快递");
                } else {
                    int odd = dao.getExpressOddByCode(code);
                    if (dao.remove(odd)) {
                        System.out.println("取件成功");
                    } else {
                        System.out.println("取件失败");
                    }
                }
            } else {
                dao.saveFile();
                break;
            }
        }
    }
}
